package com.foreverwin.jzymes.app.service.impl;

import com.foreverwin.jzymes.app.service.IResourceStatusChangeAdviceService;
import com.foreverwin.jzymes.common.service.IResourceAdviceService;
import com.foreverwin.jzymes.common.util.BOHandleUtil;
import com.foreverwin.jzymes.common.util.CommonMethods;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import com.foreverwin.jzymes.production.resource.domain.ResourceRepairLog;
import com.foreverwin.jzymes.production.resource.service.IResourceRepairLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;

@Service
public class ResourceStatusChangeAdviceService implements IResourceStatusChangeAdviceService {
    @Autowired
    IResourceAdviceService resourceAdviceService;
    @Autowired
    IResourceRepairLogService resourceRepairLogService;

    public void changeResourceStatus( String site, String resource, String status, String breakDesc ){
        /**设备报修*/
        if( "DOWN".equals( status ) ){
            Assert.hasText( breakDesc, I18nUtil.getI18nText( "production.resource.breakDescNotBlank" ) );
            ResourceRepairLog resourceRepairLog = new ResourceRepairLog();
            resourceRepairLog.setHandle( BOHandleUtil.buildResourceRepairLogHandle() );
            resourceRepairLog.setSite( site );
            resourceRepairLog.setResrce( resource );
            resourceRepairLog.setBreakDesc( breakDesc );
            resourceRepairLog.setCreateUserBo( CommonMethods.getUserBo() );
            resourceRepairLog.setCreatedDateTime( new Date() );
            resourceRepairLogService.insertResourceRepairLog( resourceRepairLog );
        }

        /**cnc设备要遵循设备状态转换规则，其他设备状态以上传状态为准*/
        if( resourceAdviceService.isCNC( site, resource ) ){
            resourceAdviceService.changeResourceStatus( site, resource, status );
        }else{
            resourceAdviceService.updateResourceStatus( site, resource, status );
        }
    }

}
